//
//  SendViewController.m
//  AntsExpress
//
//  Created by slccper on 16/1/12.
//  Copyright © 2016年 slccper. All rights reserved.
//

#import "SendViewController.h"
#import "NewSendViewController.h"
#import "clsAntsService.h"
#import "clsExpress.h"
@interface SendViewController ()<clsAntsServiceDelegate>
{
    NSMutableArray *_tableList;
    clsAntsService *ants;
}

@end

@implementation SendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(DelectAction:) name:@"DeleteSuccess" object:nil];
    UIView *allLabel = [[UIView alloc]initWithFrame:CGRectMake(0, 210, ScreenWidth, 30)];
    allLabel.backgroundColor = [UIColor colorWithRed:200/255.0f green:146/255.0f blue:5/255.0f alpha:1];
    //    allLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [allLabel addSubview:[self CreatByText:@"編號:" :0]];
    [allLabel addSubview:[self CreatByText:@"收件日:" :1]];
    [allLabel addSubview:[self CreatByText:@"單號:" :2]];
    [clsOtherFun addAllLayoutConstraints:allLabel];
    self.tableView.tableHeaderView = allLabel;
    
    _tableList = [NSMutableArray array];
//    for (int i=0;i<3; i++) {
//        clsCategory *cls = [[clsCategory alloc]init];
//        cls.Waybill=@"22011283";
//        cls.Status=@"可取货";
//        cls.Recei=@"2015.12.29";
//        cls.Date=@"2016.01.03";
//        cls.Freight=@"15";
//        [cls.subCategorys addObject:@""];
//        [_tableList addObject:cls];
//    }
    self.tableView.backgroundColor=[UIColor whiteColor];
    UIImageView * icon=[[UIImageView alloc] initWithFrame:CGRectMake(10.0, 5, 30, 30.0)];
    icon.image=[UIImage imageNamed:@"back.png"];
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 20, 50, 44);
//    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [button addSubview:icon];
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithCustomView:button];
    [self.navigationItem setLeftBarButtonItem:left animated:YES];
    self.title=@"代寄服務";
    UIButton *Rightbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    Rightbutton.frame = CGRectMake(0, 20, 80, 44);
    [Rightbutton setTitle:@"新增寄件" forState:UIControlStateNormal];
    [Rightbutton addTarget:self action:@selector(NewAction) forControlEvents:UIControlEventTouchUpInside];
    //        [button addSubview:icon];
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:Rightbutton];
    [self.navigationItem setRightBarButtonItem:right animated:YES];
    ants = [[clsAntsService alloc]init];
    ants.delegate=self;
    self.tableView.tableFooterView=[[UIView alloc]init];
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    // Do any additional setup after loading the view from its nib.
}
-(void)viewWillAppear:(BOOL)animated
{
    [self getWebdata];
}
-(void)getWebdata
{
    [clsOtherFun showLoadingView:@"正在獲取數據..."];
    [ants GetSendExpress];
}
-(void)DelectAction:(NSNotification *)sender
{
    NSDictionary *dic = (NSDictionary *)sender.object;
    NSInteger index = [_tableList indexOfObject:dic];
    [_tableList removeObjectAtIndex:index];
    [self.tableView reloadData];
//    NSIndexPath *indexpath = [NSIndexPath indexPathForRow:index inSection:0];
//    [self.tableView deleteRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationTop];
}
-(void)getAntsDataFail:(NSInteger)type
{
    [clsOtherFun hideLoadingView];
    [clsOtherFun ShowMsgWithNoData];
}
-(void)getAntsDataSuccess:(NSInteger)type :(NSMutableDictionary *)pDictionary
{
    [clsOtherFun hideLoadingView];
    NSDictionary *dic = [pDictionary objectForKey:@"data"];
    NSString *status = [dic objectForKey:@"status"];
    if([status integerValue]==404){
      [clsOtherFun ShowMsg:[dic objectForKey:@"message"]];
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    if ([status integerValue]==200) {
        _tableList = [NSMutableArray arrayWithArray:[dic objectForKey:@"data"]];
        [self.tableView reloadData];
    }
}
-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)NewAction
{
    NewSendViewController *new = [[NewSendViewController alloc]initWithNibName:@"NewSendViewController" bundle:nil];
    new.isBack=YES;
    [self.navigationController pushViewController:new animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Nested Tables methods
- (NSInteger)mainTable:(UITableView *)mainTable numberOfItemsInSection:(NSInteger)section {
    return [_tableList count];
}

- (NSInteger)mainTable:(UITableView *)mainTable numberOfSubItemsforItem:(SDGroupCell2 *)item atIndexPath:(NSIndexPath *)indexPath {
//    clsExpress *category = [_tableList objectAtIndex:indexPath.row];
    return 1;
}

- (SDGroupCell2 *)mainTable:(UITableView *)mainTable setItem:(SDGroupCell2 *)item forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *category = [_tableList objectAtIndex:indexPath.row];
    [item FillData:category];
    //    item.itemText.text = category.CategoryName;
    
    
    return item;
}

- (SDSubCell2 *)item:(SDGroupCell2 *)item setSubItem:(SDSubCell2 *)subItem forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *category = [_tableList objectAtIndex:item.cellIndex];
//    clsCategory *subcategory = [category.subCategorys objectAtIndex:indexPath.row];
    //    subItem set
    [subItem fillData:category];
    return subItem;
}

- (void) mainTable:(UITableView *)mainTable itemDidChange:(SDGroupCell2 *)item {
}

- (void) mainItem:(SDGroupCell2 *)item subItemDidChange: (SDSubCell2 *)subItem forTap:(BOOL)tapped {
    
}

-(UILabel *)CreatByText:(NSString *)text :(NSUInteger)tag
{
    UILabel *status = [[UILabel alloc]initWithFrame:CGRectZero];
    status.text=text;
    status.tag=tag;
    status.textAlignment= NSTextAlignmentCenter;
    status.translatesAutoresizingMaskIntoConstraints = NO;
    
    //    CGSize size = [status.text boundingRectWithSize:CGSizeMake(100, 30) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size;
    //    [status setFrame:CGRectMake(0, 0, size.width, 30)];
    status.font = [UIFont systemFontOfSize:15];
    status.textColor=[UIColor whiteColor];
    return status;
}

@end
